package zengjin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import zengjin.common.BeanCopyUtil;
import zengjin.common.PageResponse;
import zengjin.mapper.*;
import zengjin.model.dto.send.AddSendRequest;
import zengjin.model.dto.send.PageSendRequest;
import zengjin.model.en.*;
import zengjin.model.vo.send.JobAndResumeVO;
import zengjin.model.vo.send.PageJobAndResumeVO;
import zengjin.service.SendService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 19324
 * @description 针对表【send】的数据库操作Service实现
 * @createDate 2025-01-02 14:22:19
 */
@Service
public class SendServiceImpl extends ServiceImpl<SendMapper, Send>
        implements SendService {

    @Resource
    private SendMapper sendMapper;

    @Resource
    private ImageMapper imageMapper;

    @Resource
    private JobMapper jobMapper;

    @Resource
    private ResumeMapper resumeMapper;

    @Resource
    private JobseekerMapper jobseekerMapper;


    @Override
    public boolean addSend(AddSendRequest addSendRequest) {
        //1. 获取参数
        Long sendJobseekerId = addSendRequest.getSendJobseekerId();
        Long sendEmployerId = addSendRequest.getSendEmployerId();
        Long sendJobId = addSendRequest.getSendJobId();
        String sendStatus = addSendRequest.getSendStatus();

        //2. 构造实体类
        Send send = new Send();
        send.setSendJobseekerId(sendJobseekerId);
        send.setSendEmployerId(sendEmployerId);
        send.setSendJobId(sendJobId);
        send.setSendStatus(sendStatus);

        //3. 保存
        return sendMapper.insert(send) > 0;


    }

    @Override
    public long isSend(Long sendJobseekerId, Long sendEmployerId, Long sendJobId) {
        //1. 构造条件
        QueryWrapper<Send> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("send_jobseeker_id", sendJobseekerId)
                .eq("send_employer_id", sendEmployerId)
                .eq("send_job_id", sendJobId);
        //2. 查询
        Send send = sendMapper.selectOne(queryWrapper);
        if (send == null) {
            return 0;
        }
        return send.getSendId();
    }

    @Override
    public boolean updateSendStatus(Long sendId, String sendStatus) {
        //1. 构造条件
        LambdaUpdateWrapper<Send> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Send::getSendId, sendId)
                .set(Send::getSendStatus, sendStatus);
        //2. 更新
        return sendMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public PageResponse<PageJobAndResumeVO> pageSend(PageSendRequest pageSendRequest) {
        //1.获取参数
        String sendStatus = pageSendRequest.getSendStatus();
        Long sendEmployerId = pageSendRequest.getSendEmployerId();
        String resumeGender = pageSendRequest.getResumeGender();
        String resumeEducationDegree = pageSendRequest.getResumeEducationDegree();
        String searchName = pageSendRequest.getSearchName();

        //2.将企业的投递记录查询出来
        QueryWrapper<Send> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("send_employer_id", sendEmployerId);
        queryWrapper.eq("send_status", sendStatus);
        List<Send> sends = sendMapper.selectList(queryWrapper);

        //3.将投递记录中的求职者和岗位查询出来
        List<PageJobAndResumeVO> pageJobAndResumeVOSList = new ArrayList<>();
        sends.forEach(send -> {
            long sendJobseekerId = send.getSendJobseekerId();
            long sendJobId = send.getSendJobId();
            Resume resume = resumeMapper.selectOne(new QueryWrapper<Resume>().eq("resume_jobseeker_id", sendJobseekerId));
            Job job = jobMapper.selectById(sendJobId);
            //条件筛选
            if ( !"".equals(resumeGender) && !resumeGender.equals(resume.getResumeGender())) {
                return;
            }
            if (!"".equals(resumeEducationDegree) && !resumeEducationDegree.equals(resume.getResumeEducationDegree())) {
                return;
            }
            if (!"".equals(searchName) && (!searchName.equals(resume.getResumeUsername()) && !searchName.equals(job.getJobTitle()))) {
                return;
            }
            //将数据封装到PageJobAndResumeVO中
            PageJobAndResumeVO pageJobAndResumeVO = BeanCopyUtil.copyPropertiesToVo(resume, job, PageJobAndResumeVO.class);
            pageJobAndResumeVO.setSendId(send.getSendId());
            //把简历图片查询出来
            QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
            imageQueryWrapper.eq("image_name_id", resume.getResumeId());
            imageQueryWrapper.eq("image_name", "resume_avatar");
            Image image = imageMapper.selectOne(imageQueryWrapper);
            if (image != null) {
                pageJobAndResumeVO.setResumeAvatar(image.getImageUrl());
            }
            pageJobAndResumeVOSList.add(pageJobAndResumeVO);

        });
        //4.分页
        Page<PageJobAndResumeVO> page = pageSendRequest.toPageDefaultSortByUpdateTime();


        return PageResponse.of(page, pageJobAndResumeVOSList);

    }

    @Override
    public JobAndResumeVO getSendById(Long sendId) {
        //1.查询出投递记录
        Send send = sendMapper.selectById(sendId);
        //2.查询出岗位，简历
        Long sendJobseekerId = send.getSendJobseekerId();
        Long sendJobId = send.getSendJobId();
        Job job = jobMapper.selectById(sendJobId);
        QueryWrapper<Resume> resumeQueryWrapper = new QueryWrapper<>();
        resumeQueryWrapper.eq("resume_jobseeker_id", sendJobseekerId);
        Resume resume = resumeMapper.selectOne(resumeQueryWrapper);
        //3.封装数据
        JobAndResumeVO jobAndResumeVO = BeanCopyUtil.copyPropertiesToVo(resume, job, JobAndResumeVO.class);
        jobAndResumeVO.setJobseekerId(String.valueOf(sendJobseekerId));
        //4.查询出简历图片
        QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("image_name_id", resume.getResumeId());
        imageQueryWrapper.eq("image_name", "resume_avatar");
        Image image = imageMapper.selectOne(imageQueryWrapper);
        if (image != null) {
            jobAndResumeVO.setResumeAvatar(image.getImageUrl());
        }
        return jobAndResumeVO;
    }
}




